package org.jash.shop2202.entity

import androidx.databinding.BaseObservable
import androidx.databinding.Bindable

class Cart:BaseObservable() {
    val data = mutableListOf<CartItem>()
    @get:Bindable
    var allChecked:Int
        get() = when(data.filter(CartItem::checked).size){
            0 -> 0
            data.size -> 1
            else -> 2
        }
        set(value) {
            if (value != 2) {
                data.forEach { it.checked = value == 1 }
            }
        }
    @get:Bindable("allChecked")
    val total:Float
        get() = data.filter(CartItem::checked).map { it.goods_default_price * it.count }.sum()
    @get:Bindable("allChecked")
    val coupon:Float
        get() = total * 0.8f
}